/**

 * http://mongodb.github.io/node-mongodb-native

 * http://mongodb.github.io/node-mongodb-native/3.0/api/
 */

//DB库
const MongoDB = require('mongodb');
const MongoClient = MongoDB.MongoClient;
const ObjectID = MongoDB.ObjectID;
const Config = require('../../config/config')
let dbURL = "mongodb://" + Config.mongodb.db_user + ":" + Config.mongodb.db_pwd + "@" + Config.mongodb.db_host + ":" + Config.mongodb.db_port + "/" + Config.mongodb.db_name
class Db {
    static getInstance() {   /*1、单例  多次实例化实例不共享的问题*/
        if (!Db.instance) {
            Db.instance = new Db();
        }
        return Db.instance;
    }
    constructor() {
        this.dbClient = ''; /*属性 放db对象*/
        this.connect();   /*实例化的时候就连接数据库*/

    }
    connect() {  /*连接数据库*/
        let _that = this;
        return new Promise((resolve, reject) => {
            if (!_that.dbClient) {         /*1、解决数据库多次连接的问题*/
                MongoClient.connect(dbURL, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => {
                    if (err) {
                        reject(err)
                    } else {
                        _that.dbClient = client.db(Config.mongodb.db_name);
                        resolve(_that.dbClient)
                    }
                })
            } else {
                resolve(_that.dbClient);
            }
        })
    }
    /**
     * 多个查询
     * @param {*} collectionName 表名
     * @param {*} json where
     * @param {*} sort 排序
     * @param {*} skip 开始
     * @param {*} limit 数量
     */
    find(collectionName, json = {}, sort = {}, skip = 0, limit = 0) {
        return new Promise((resolve, reject) => {
            this.connect().then((db) => {
                let result = db.collection(collectionName).find(json);
                if (skip > 0) result = result.skip(skip)
                if (limit > 0) result = result.limit(limit)
                if (sort != {}) result = result.sort(sort)
                result.toArray(function (err, docs) {
                    if (err) {
                        reject(err);
                        return;
                    }
                    resolve(docs);
                })
            })
        })
    }
    //聚合
    aggregate(collectionName, json = {}) {
        return new Promise((resolve, reject) => {
            this.connect().then((db) => {
                let result = db.collection(collectionName).aggregate(json);
                result.toArray(function (err, docs) {
                    if (err) {
                        reject(err);
                        return;
                    }
                    resolve(docs);
                })

            })
        })
    }
    update(collectionName, json1, json2, json3 = {}) {
        return new Promise((resolve, reject) => {
            this.connect().then((db) => {
                db.collection(collectionName).updateOne(json1, { $set: json2 }, json3, (err, result) => {
                    if (err) {
                        reject(err);
                    } else {
                        resolve(result);
                    }
                })
            })
        })
    }
    updates(collectionName, json1, json2) {
        return new Promise((resolve, reject) => {
            this.connect().then((db) => {
                db.collection(collectionName).updateMany(json1, json2, (err, result) => {
                    if (err) {
                        reject(err);
                    } else {
                        resolve(result);
                    }
                })
            })
        })
    }
    insert(collectionName, json) {
        return new Promise((resolve, reject) => {
            this.connect().then((db) => {
                db.collection(collectionName).insertOne(json, function (err, result) {
                    if (err) {
                        reject(err);
                    } else {
                        resolve(result);
                    }
                })
            })
        })
    }
    insertMany(collectionName, json){
        return new Promise((resolve, reject) => {
            this.connect().then((db) => {
                db.collection(collectionName).insertMany(json, function (err, result) {
                    if (err) {
                        reject(err);
                    } else {
                        resolve(result);
                    }
                })
            })
        })
    }
    remove(collectionName, json) {
        return new Promise((resolve, reject) => {
            this.connect().then((db) => {
                db.collection(collectionName).removeOne(json, function (err, result) {
                    if (err) {
                        reject(err);
                    } else {
                        resolve(result);
                    }
                })
            })
        })
    }
    deleteMany(collectionName, json) {
        return new Promise((resolve, reject) => {
            this.connect().then((db) => {
                db.collection(collectionName).deleteMany(json, function (err, result) {
                    if (err) {
                        reject(err);
                    } else {
                        resolve(result);
                    }
                })
            })
        })
    }
    getObjectId(id) {    /*mongodb里面查询 _id 把字符串转换成对象*/
        return new ObjectID(id);
    }
}
module.exports = Db.getInstance();